package cn.project.lemontree.controller;


import cn.project.lemontree.aop.SystemControllerLog;
import cn.project.lemontree.bean.Child;
import cn.project.lemontree.bean.Red_flower;
import cn.project.lemontree.service.ChildService;
import cn.project.lemontree.service.Red_flowerService;
import cn.project.lemontree.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/redflower")
@Api(tags = "小红花管理模块")
@CrossOrigin
public class Red_flowerController {
    /**
     * 服务对象
     */
    @Resource
    private Red_flowerService redFlowerService;
    ChildService childService;


    @ApiOperation("分页查询小红花信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="childName",value="儿童名称",required=false,dataTypeClass = String.class)
    })
    @GetMapping("/listpage")
    public CommonResult listpage(int page,int size,String childName){
        Map<String,Object> map = new HashMap<>();
        map.put("count",redFlowerService.count(childName));
        map.put("data",redFlowerService.listPage(page,size,childName));
        return CommonResult.success(map);
    }

    @SystemControllerLog(description = "删除小红花")
    @ApiOperation("删除小红花信息")
    @PostMapping("/remove")
    public CommonResult remove(@RequestParam("redFlower_id") int redFlower_id){
        return CommonResult.success(redFlowerService.removeById(redFlower_id));
    }

    @SystemControllerLog(description = "添加小红花")
    @ApiOperation("添加小红花信息")
    @PostMapping("/save")
    public CommonResult save(@RequestBody RedFlowerChildId redFlowerChildId){
        Child child = new Child();
        child.setChildName(redFlowerChildId.getChildName());
        Red_flower redFlower = redFlowerChildId.getRedFlower();
        redFlower.setChild(child);
        return CommonResult.success(redFlowerService.save(redFlower));
    }
    static class RedFlowerChildId implements Serializable {
        private Red_flower redFlower;
        private String childName;
        public Red_flower getRedFlower(){
            return this.redFlower;
        }
        public String getChildName(){
            return this.childName;
        }
    }
}

